<template>
  <div class="home-new" ref="target">
    <HomePanel title="新鲜好物" subTitle="新鲜出炉 品质靠谱">
      <template #right><XtxMore path="/" /></template>
      <!-- 面板内容 -->
      <ul class="goods-list">
        <li v-for="item in goods" :key="item.id">
          <RouterLink :to="`/product/${item.id}`">
            <img :src="item.picture" alt="" />
            <p class="name ellipsis">{{ item.name }}</p>
            <p class="price">{{ item.price }}</p>
          </RouterLink>
        </li>
      </ul>
    </HomePanel>
  </div>
</template>
<script>
import HomePanel from './HomePanel.vue'
import { ref } from 'vue'
import { findNew } from '@/api/home'
// useIntersectionObserver来判断是否进入可视区域
import { useIntersectionObserver } from '@vueuse/core'
export default {
  name: 'HomeNew',
  components: { HomePanel },
  setup() {
    const target = ref(null)
    const goods = ref([])
    // useIntersectionObserver函数来判断是否进入可视区域
    useIntersectionObserver(
      target,
      ([{ isIntersecting }], observerElement) => {
        // isIntersecting 是否进入可视区域，true是  false否
        // observerElement 被观察的dom元素
        if (isIntersecting) {
          stop()
          //  发ajax
          console.log(isIntersecting)
          findNew().then(item => {
            goods.value = item.result
          })
        }
      },
      // threshold 表示观察的目标进入视口的比例，取值范围是从 0 到 1
      // 默认是0.1。 值越大表示观察越不灵敏,如果是0.1可以省略不写
      { threshold: 0.1 }
    )
    return { goods, target }
  }
}
</script>
<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding: 12px 30px 0 30px;
      text-align: center;
    }
    .price {
      color: @priceColor;
    }
  }
}
</style>
